package org.cheetahworkflow.designer.utils;

import java.util.Comparator;

import org.cheetahworkflow.designer.model.Comment;
import org.cheetahworkflow.designer.model.EndNode;
import org.cheetahworkflow.designer.model.ModuleNode;
import org.cheetahworkflow.designer.model.Node;
import org.cheetahworkflow.designer.model.SequenceNode;
import org.cheetahworkflow.designer.model.StartNode;
import org.cheetahworkflow.designer.model.SwitchNode;

public class NodeSortor implements Comparator<Node> {
	
	private static NodeSortor sortor = new NodeSortor();
	
	public static NodeSortor getInstance() {
		return sortor;
	}
	
	private static int getOrderIndex(Node node) {
		if (node instanceof StartNode) {
			return 0;
		} else if (node instanceof SequenceNode || node instanceof SwitchNode || node instanceof ModuleNode) {
			return 1;
		} else if (node instanceof EndNode) {
			return 2;
		} else if (node instanceof Comment) {
			return 3;
		} else {
			throw new IllegalArgumentException("Cannot identify the type of node:" + node);
		}
	}

	@Override
	public int compare(Node node1, Node node2) {
		int order1 = getOrderIndex(node1);
		int order2 = getOrderIndex(node2);
		return order1 - order2;
	}
}
